package web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import vo.Topic;
import vo.User;
import vo.UserTopic;
import business.TopicService;
import business.UserService;
import business.UserTopicService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class UserTopicAction implements Preparable {
	private UserTopicService service;
	private TopicService topicService;
	private UserService userService;
	private List<UserTopic> userTopics;
	// private List<UserTopic> nonSubUsers;
	private List<User> users;

	private ArrayList<String> SelectedItem = new ArrayList<String>();// 已选择用户的id列表
	private ArrayList<String> WaitSelectItem = new ArrayList<String>();// 已选择用户的id列表

	private String topicId;
	private String topicName;
	private User curUser;

	// 显示主题订阅的列表
	public String listUserTopic() {
		System.out.println("topicId:" + topicId);

		topicName = topicService.getById(Long.parseLong(topicId)).getName();
		System.out.println("topicName:" + topicName);

		// 订阅的用户( 某主题下，普通的用户)
		userTopics = service.getByTopicIdUserRole(Long.parseLong(topicId));
		System.out.println("订阅用户:" + userTopics.size());

		// 未订阅(所有-订阅的)
		List<User> allUsers = userService.getAllUsersUserRole();
		System.out.println("所有未订阅的普通用户:" + allUsers.size());
		users = new ArrayList<User>();
		for (int i = 0; i < allUsers.size(); i++) {
			if (!isIn(allUsers.get(i), userTopics))
				users.add(allUsers.get(i));
		}
		System.out.println("未订阅用户:" + users.size());
		return Action.SUCCESS;
	}

	private boolean isIn(User user, List<UserTopic> userTopics) {
		for (int i = 0; i < userTopics.size(); i++) {
			if (user.getId().longValue() == userTopics.get(i).getUser().getId().longValue()) {
				return true;
			}
		}

		return false;
	}

	public String addUserTopic() {
		int topicId = 4;
		int userId = 5;

		Topic topic = topicService.getById(topicId);
		User user = userService.getById(userId);

		UserTopic userTopic = new UserTopic();
		userTopic.setTopic(topic);
		userTopic.setUser(user);
		service.addUserTopic(userTopic);
		return listUserTopic();
	}

	// 主题订阅用户
	public String saveUserTopic() {
		System.out.println("topicId:" + topicId);

		// selected的列表（if用户没订阅，订阅；否则啥都不做）
		for (String item : SelectedItem) {
			System.out.println("选择列表 user id:" + item);
			// 如果用户没有订阅主题，那么订阅
			if (!service.isExist(Long.parseLong(item), Long.parseLong(topicId))) {
				System.out.println("insert user topic:" + item + "," + topicId);
				UserTopic userTopic = new UserTopic();
				userTopic.setTopic(topicService.getById(Long.parseLong(topicId)));
				userTopic.setUser(userService.getById(Long.parseLong(item)));
				service.addUserTopic(userTopic);
			}
		}
		System.out.println("WaitSelectItem:" + WaitSelectItem.size());

		// unselected列表(如果已定阅，删除；否则啥都不做)
		for (String item : WaitSelectItem) {
			System.out.println("未选择列表 user id:" + item);
			//如果用户订阅了主题，那么删除
			if (service.isExist(Long.parseLong(item), Long.parseLong(topicId))) {
				UserTopic ut = service.getByTopicIdUserId(Long.parseLong(topicId), Long.parseLong(item));
				service.delete(ut.getId());
				System.out.println("删除  user topic:" + item + "," + topicId);
			}
		}
		return "saveUserTopic";
	}

	public List<UserTopic> getUserTopics() {
		return userTopics;
	}

	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		curUser = userService.getByLoginName(username);
		System.out.println("curUser.getLoginName():" + curUser.getLoginName());
	}

	public void setUserTopicService(UserTopicService service) {
		this.service = service;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ArrayList<String> getSelectedItem() {
		return SelectedItem;
	}

	public void setSelectedItem(ArrayList<String> selectedItem) {
		SelectedItem = selectedItem;
	}

	public String getTopicId() {
		return topicId;
	}

	public void setTopicId(String topicId) {
		this.topicId = topicId;
	}

	public String getTopicName() {
		return topicName;
	}

	public void setTopicName(String topicName) {
		this.topicName = topicName;
	}

	public void setUserTopics(List<UserTopic> userTopics) {
		this.userTopics = userTopics;
	}

	public void setService(UserTopicService service) {
		this.service = service;
	}

	// public List<UserTopic> getNonSubUsers() {
	// return nonSubUsers;
	// }
	//
	// public void setNonSubUsers(List<UserTopic> nonSubUsers) {
	// this.nonSubUsers = nonSubUsers;
	// }

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public ArrayList<String> getWaitSelectItem() {
		return WaitSelectItem;
	}

	public void setWaitSelectItem(ArrayList<String> waitSelectItem) {
		WaitSelectItem = waitSelectItem;
	}

}